<!-- Page heading -->
<div class="page-head">
    <h2 class="pull-left"><i class="fa fa-building-o"></i> Stok Material</h2>
    <!-- Breadcrumb -->
    <div class="bread-crumb pull-right">
        <a href="index.html"><i class="fa fa-home"></i>Home</a>
        <!-- Divider -->
        <span class="divider">/</span> 
        <a href="#" class="bread-current">History</a>
    </div>
    <div class="clearfix"></div>
</div>
<!-- Page heading ends -->

<div class="col-md-12" id="div_header1">
    <div class="widget wgreen">
        <div class="widget-head">
            <div class="pull-left">Daftar stok di <?= ($kategori == 'rab' ? 'RAB ' : 'Gudang ') ?> <?= $nama_kategori ?></b></div>
            <div class="widget-icons pull-right">
                <a href="#" class="wminimize">
                    <i class="fa fa-chevron-up"></i>
                </a>
            </div>
            <div class="clearfix"></div>
        </div>
        <div class="widget-content" style="padding: 10px">
            <div class="padd">
                <a href="<?= base_url() . $this->uri->segment(1, 0) . '/' . $this->uri->segment(2, 0) ?>/stok_kategori_PDF?id_jenis=<?= $id_kategori ?>&jenis=<?= $kategori ?>" target="_blank"><img src="<?= base_url(); ?>assets/default/img/icons/pdf.png" class="icons" title="Export to PDF" /></a>
            </div>
            <div class="page-tables">
                <div class="table-responsive">
                    <div class="clearfix"></div>
                    <table class="table table-striped table-bordered table-hover" id="tabel_stok" style="">
                        <thead>
                            <tr>
                                <th>No.</th>
                                <th>Kode Material</th>
                                <th>Nama Material</th>
                                <th>Jumlah Stok</th>
                                <th>Satuan </th>
                                <th>Harga Satuan Rata-Rata</th>
                                <th>Nilai Aset</th>
                                <th>Aksi</th>
                            </tr>
                        </thead>
                        <tbody class="" id="tabel_stok_body">
                        </tbody>
                    </table>
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var base_url1 = '<?= base_url() ?>';
    var uri = '<?= $this->uri->segment(1, 0) . '/' . $this->uri->segment(2, 0) ?>';
    var base_url = base_url1 + uri;
    $(document).ready(function () {
        document.getElementsByClassName('has_sub')[2].setAttribute('class', 'has_sub open');
<?php
if ($kategori == 'rab') {
    ?>
            req_stok_rab('<?= $id_kategori ?>', 'tabel_stok');
    <?php
} else if ($kategori == 'gudang') {
    ?>
            req_stok_gudang('<?= $id_kategori ?>', 'tabel_stok');
    <?php
}
?>
    });
    var datatable_gudang = null;
    function req_stok_gudang(id_gudang, tabel_id_target) {
        if (datatable_gudang != null) {
            datatable_gudang.fnDestroy();
        }
        datatable_gudang = $('#' + tabel_id_target).dataTable({
            "processing": true,
            "serverSide": true,
            "ajax": {
                'method': 'post',
                "url": base_url + "/get_stok_gudang_datatable",
                "data": {
                    id_gudang: id_gudang
                },
                "dataSrc": function (json) {
                    console.log(json);
                    jsonData = json.data;
                    return json.data;
                }
            },
            "createdRow": function (row, data, index) {
                var id = data[0];
                $('td', row).eq(0).html(index + 1);
                var kode = data[2],
                        nama = data[3],
                        volume = data[1],
                        satuan = data[4],
                        id_barang = data[5],
                        id_subcon = data[6],
                        id_overhead = data[7],
                        nilai_aset = data[8];

                $('td', row).eq(1).html(kode);
                $('td', row).eq(2).html(nama);
                $('td', row).eq(3).html(volume);
                $('td', row).eq(4).html(satuan);
                $('td', row).eq(5).html((parseFloat(nilai_aset) / parseFloat(volume)).formatMoney());
                $('td', row).eq(6).html(parseFloat(nilai_aset).formatMoney());
                $('td', row).eq(7).html('<button class="btn btn-xs btn-success" onclick="view_barang(\'gudang\',' + id_gudang + ',' + id_barang + ',' + id_subcon + ',' + id_overhead + ')"><i class="fa fa-plus fa-fw"></i> Lihat History</button>');
                $(row).attr('id', 'item_' + id);
            }
        });
    }
    var datatable_rab = null;
    function req_stok_rab(id_rab, tabel_id_target) {
        if (datatable_rab != null) {
            datatable_rab.fnDestroy();
        }
        datatable_rab = $('#' + tabel_id_target).dataTable({
            "processing": true,
            "serverSide": true,
            "ajax": {
                'method': 'post',
                "url": base_url + "/get_stok_rab_datatable",
                "data": {
                    id_rab: id_rab
                },
                "dataSrc": function (json) {
                    console.log(json);
                    jsonData = json.data;
                    return json.data;
                }
            },
            "createdRow": function (row, data, index) {
                var id = data[0];
                $('td', row).eq(0).html(index + 1);
                var kode = data[2],
                        nama = data[3],
                        volume = data[1],
                        satuan = data[4],
                        id_barang = data[5],
                        id_subcon = data[6],
                        id_overhead = data[7],
                        nilai_aset = data[8];
                $('td', row).eq(1).html(kode);
                $('td', row).eq(2).html(nama);
                $('td', row).eq(3).html(volume);
                $('td', row).eq(4).html(satuan);
                $('td', row).eq(5).html((parseFloat(nilai_aset) / parseFloat(volume)).formatMoney());
                $('td', row).eq(6).html(parseFloat(nilai_aset).formatMoney());
                $('td', row).eq(7).html('<button class="btn btn-xs btn-success" onclick="view_barang(\'rab\',' + id_rab + ',' + id_barang + ',' + id_subcon + ',' + id_overhead + ')"><i class="fa fa-plus fa-fw"></i> Lihat History</button>');
                $(row).attr('id', 'item_' + id);
            }
        });
    }
    function view_barang(tipe, id_tipe, id_barang, id_subcon, id_overhead) {
        var win = window.open(base_url + '/history?id_barang=' + id_barang + '&id_subcon=' + id_subcon + '&id_overhead=' + id_overhead + '&jenis_history=' + tipe + '&id_tipe=' + id_tipe, '_blank');
        win.focus();
    }
</script>